package com.cmcc.passenger.ui.profile

import android.os.Bundle
import android.view.View
import androidx.databinding.DataBindingUtil
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProviders
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.cmcc.passenger.R
import com.cmcc.passenger.config.PassPath
import com.cmcc.passenger.databinding.ActivityPassengerMsglistBinding
import com.cmcc.passenger.ui.base.BasicActivity
import com.cmcc.passenger.ui.fragment.AppProgressDialog
import com.cmcc.passenger.ui.profile.viewmodel.ProfileViewModel
import com.cmcc.shared.SdkManager
import com.cmcc.shared.model.Msg
import com.cmcc.shared.utils.DisplayUtil
import com.cmcc.shared.utils.RecyclerViewSpacesItemDecoration
import com.cmcc.shared.utils.showToast


@Route(path = PassPath.MSG_LIST)
class PassengerMsgListActivity : BasicActivity() {
    private lateinit var viewModel: ProfileViewModel
    private lateinit var binding: ActivityPassengerMsglistBinding

    private var progressDialog: AppProgressDialog? = null

    private var adapter: PassengerMsgAdapter? = null
    private var lastPage: Int = 0
    private var currentPage: Int = 1
    private val pageSize = 10
    private var lastVisibleItemPosition: Int = -1
    /**
     * 初始化
     */
    override fun initialize(savedInstanceState: Bundle?) {
        progressDialog = AppProgressDialog(this)
        viewModel = ViewModelProviders.of(this).get(ProfileViewModel::class.java)
        binding = DataBindingUtil.setContentView(this, R.layout.activity_passenger_msglist)

        binding.appbar.title.text = getString(R.string.sys_msg)

        binding.appbar.toolbar.setNavigationOnClickListener { finish() }

        binding.recyclerView.layoutManager = LinearLayoutManager(this)

        val stringIntegerHashMap = HashMap<String, Int>()
        //设置下间距
        stringIntegerHashMap.put(
            RecyclerViewSpacesItemDecoration.BOTTOM_DECORATION,
            DisplayUtil.dip2px(this, 14f)
        )
        binding.recyclerView.addItemDecoration(RecyclerViewSpacesItemDecoration(stringIntegerHashMap))



        adapter = PassengerMsgAdapter()
        updateUI()
        adapter?.addItemClickListener(onItemClickListener())
        binding.recyclerView.adapter = adapter
        viewModel = ViewModelProviders.of(this).get(ProfileViewModel::class.java)

        binding.recyclerView.addOnScrollListener(object : RecyclerView.OnScrollListener() {
            override fun onScrollStateChanged(recyclerView: RecyclerView, newState: Int) {
                super.onScrollStateChanged(recyclerView, newState)
                val layoutManager = recyclerView.layoutManager
                if (layoutManager is LinearLayoutManager) {
                    lastVisibleItemPosition =
                        layoutManager.findLastVisibleItemPosition()
                }
                if (newState == RecyclerView.SCROLL_STATE_IDLE
                    && lastVisibleItemPosition + 1 == recyclerView.adapter?.itemCount
                ) {
                    if (currentPage > lastPage)
                        viewModel.list(currentPage, pageSize);
                }

            }
        })
        binding.swipeRefreshLayout.setOnRefreshListener {
            updateUI()
        }

        viewModel.msgList.observe(this, Observer { msgBean ->
            binding.swipeRefreshLayout.isRefreshing = false
            if (msgBean.code == 200 && msgBean.data != null) {
                lastPage = currentPage
                if (currentPage == 1) {
                    adapter?.removeList()
                }
                if (currentPage <= msgBean.data.pages!!) {
                    adapter?.submitList(msgBean.data.records)
                    currentPage++
                }
                adapter?.notifyDataSetChanged()

                if (adapter?.itemCount == 0) {
                    binding.noMsgLayout.visibility = View.VISIBLE
                    binding.swipeRefreshLayout.visibility = View.GONE
                } else {
                    binding.noMsgLayout.visibility = View.GONE
                    binding.swipeRefreshLayout.visibility = View.VISIBLE
                }
            } else {
                currentPage = 1
                binding.noMsgLayout.visibility = View.VISIBLE
                binding.swipeRefreshLayout.visibility = View.GONE
            }


        })

    }

    var mLastClickTime: Long = 0
    val TIME_INTERVAL = 1000L
    private fun onItemClickListener(): MsgItemClickListener {
        return object : MsgItemClickListener {
            override fun click(
                binding: View,
                model: Msg?
            ) {
                //应该是防止多次点击
                val nowTime = System.currentTimeMillis()
                if (nowTime - mLastClickTime > TIME_INTERVAL) {
                    mLastClickTime = nowTime
                    ARouter.getInstance().build(PassPath.MSG_DETAIL)
                        .withString("content", model?.content)
                        .navigation()
                }
            }
        }
    }

    fun updateUI() {
        binding.swipeRefreshLayout.isRefreshing = true
        viewModel.list(currentPage, pageSize);


    }




}